<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button v-auth="['mp:user:sync']" type="primary" :preIcon="IconEnum.RESET" @click="handleSync">
          {{ t('action.sync') }}
        </a-button>
      </template>
      <template #bodyCell="{ column }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[{ icon: IconEnum.EDIT, label: t('action.edit'), auth: 'mp:user:update', onClick: handleEdit.bind(null) }]"
          />
        </template>
      </template>
    </BasicTable>
    <MpUserModal @register="registerModal" @success="reload()" />
  </div>
</template>
<script lang="ts" setup name="MpUser">
import { getUserPage, syncUser } from '@/api/mp/mpuser';
import { useModal } from '@/components/Modal';
import { BasicTable, TableAction, useTable } from '@/components/Table';
import { IconEnum } from '@/enums/appEnum';
import { useI18n } from '@/hooks/web/useI18n';
import { useMessage } from '@/hooks/web/useMessage';

import { columns, searchFormSchema } from './mpuser.data';
import MpUserModal from './MpUserModal.vue';

const { t } = useI18n();
const { createConfirm, createMessage } = useMessage();
const [registerModal, { openModal }] = useModal();

const [registerTable, { getForm, reload }] = useTable({
  title: '公众号账号列表',
  api: getUserPage,
  columns,
  formConfig: { labelWidth: 120, schemas: searchFormSchema },
  useSearchForm: true,
  showTableSetting: true,
  actionColumn: {
    width: 140,
    title: t('common.action'),
    dataIndex: 'action',
    fixed: 'right',
  },
});

/** 同步按钮操作 */
async function handleSync() {
  createConfirm({
    title: t('action.sync'),
    iconType: 'warning',
    content: '是否确认同步粉丝?',
    async onOk() {
      await syncUser(getForm().getFieldsValue().accountId);
      createMessage.success('开始从微信公众号同步粉丝信息，同步需要一段时间，建议稍后再查询');
    },
  });
}

/** 修改按钮操作 */
function handleEdit(record: Recordable) {
  openModal(true, { record });
}
</script>
